#include "xapp_config.h"

// always requires an idle view 
#include "kb.h"
#include "touch_algorithm.h"
#include "custom_event.h"
#include "gui.h"

#include "views.h"
#include "views_util.h"

#include "xapp_ble.h"
#include "xapp_clock.h"
#include "xapp_exercise.h"
#include "xapp_localize.h"
#include "xapp_timer.h"

#include "log.h"
#define LOG     xLOG_INFO0
#define LOG1    xLOG_INFO1
#define LOG2    xLOG_INFO2
#define LOG3    xLOG_INFO3
#define LOG4    xLOG_INFO4

enum tagApplistSelect
{
    tagApplistSelectItemWatchfacePreview, // 表盘预览
    tagApplistSelectItemExercise,         // 运动
    tagApplistSelectItemHeartrate,        // 心率测量
//    tagApplistSelectItemBloodpressure,    // 血压测量
#if !defined(BLE_NO_CONNECT)
    tagApplistSelectItemMessageList,          // 消息界面
#endif
    tagApplistSelectItemBrightness,       // 亮度调节
#if !defined(BLE_NO_CONNECT)
//    tagApplistSelectItemStopwatch,        // 秒表
    tagApplistSelectItemTimer,            // 倒计时
    tagApplistSelectItemCamera,           // 拍照
    tagApplistSelectItemMusicControl,     // 音乐控制 
    tagApplistSelectItemFindPhone,           // 查找手机
#endif
    tagApplistSelectItemLanguage,         // 语言

    tagApplistSelectItemTimeSet,          // 时间设置
    tagApplistSelectItemDateSet,          // 日期设置
    
    tagApplistSelectItemDeviceInformation,// 设备信息
#if !defined(BLE_NO_CONNECT)
    tagApplistSelectItemAppDownload,      // App下载
#endif
    tagApplistSelectItemSetUnwearTip,        // 佩戴提醒
    tagApplistSelectItemMore,             // 关机和恢复出厂设置放在更多里面

    tagApplistSelectItemCount,            // item数 

    tagApplistSelect_DragStartX,
    tagApplistSelect_ToLeft,
    tagApplistSelect_ToRight,
    tagApplistSelect_BackToWatchface,
};

enum tagItem
{
    tagItem0 = 0,
    tagItem1,
    tagItem2,
    tagItem3,
    tagItem4,
    tagItem5,
    tagItemCount
}; 

#define ITEM_HEIGHT         (GUI_LCD_RES_V/4)
#define ROLL_V_MAX_OFFSET     (1 * ITEM_HEIGHT)
#define ROLL_V_MIN_OFFSET     (-(ITEM_HEIGHT * (tagApplistSelectItemCount - 2)))
// #define ROLL_TOTAL_OFFSET       (ROLL_V_MAX_OFFSET - ROLL_V_MIN_OFFSET)

// #define IDX_MINIMUM     (-1)

#define IDX_MAXIMUM     (tagApplistSelectItemCount)

GUI_VIEW_FUNC_DECLARE_ALL;
static void local_view_event_handler( uint32_t tag ); 
static void list_item_handler(uint32_t tag); 

typedef struct
{
    uint8_t tag;
    uint8_t idx;
    uint16_t text;
}item_setting_t;

static const item_setting_t item_list[] = 
{
    { tagApplistSelectItemWatchfacePreview,           18,      TEXT_TAG_APP_WATCHFACE },
    { tagApplistSelectItemExercise,                   17,      TEXT_TAG_APP_EXERCISE },
    { tagApplistSelectItemHeartrate,                  6,       TEXT_TAG_APP_HEARTRATE },
//    { tagApplistSelectItemBloodpressure,              23,      TEXT_TAG_APP_BLOODPRESURE },
#if !defined(BLE_NO_CONNECT)
    { tagApplistSelectItemMessageList,                21,      TEXT_TAG_APP_MESSAGES },
#endif
 
    { tagApplistSelectItemBrightness,                 0,       TEXT_TAG_APP_BRIGHTNESS },
#if !defined(BLE_NO_CONNECT)
//    { tagApplistSelectItemStopwatch,                  7,       TEXT_TAG_APP_STOPWATCH },
    { tagApplistSelectItemTimer,                      12,      TEXT_TAG_APP_TIMER },
    { tagApplistSelectItemCamera,                     9,       TEXT_TAG_APP_CAMERA },
    { tagApplistSelectItemMusicControl,               11,      TEXT_TAG_APP_MUSIC },
    { tagApplistSelectItemFindPhone,                  4,       TEXT_TAG_APP_FIND_PHONE },
#endif
    { tagApplistSelectItemLanguage,                   10,      TEXT_TAG_APP_LANGUAGE },
    
    { tagApplistSelectItemTimeSet,                     26,     TEXT_TAG_SET_TIME },
    { tagApplistSelectItemDateSet,                     27,     TEXT_TAG_SET_DATE },
    
    { tagApplistSelectItemDeviceInformation,          2,       TEXT_TAG_APP_DEVICE },
#if !defined(BLE_NO_CONNECT)
    { tagApplistSelectItemAppDownload,                24,      TEXT_TAG_APP_QRCODE },
#endif
    { tagApplistSelectItemSetUnwearTip,               28,      TEXT_TAG_UNWEAR_TIP },
    { tagApplistSelectItemMore,                       13,      TEXT_TAG_WATCH_POWER_OFF/*TEXT_TAG_APP_MORE*/ },
};


const static ui_widgets_type_t wglSetting[] = 
{
    //background
    [0] = uiCanvas(0,0, GUI_LCD_RES_H, GUI_LCD_RES_V, COLOR_BLACK ),

    //progress bar 
    [1] = uiCanvas( 0, ITEM_HEIGHT * 1, GUI_LCD_RES_H, ITEM_HEIGHT, COLOR_GRAY2 ), // 用于按下位置

    // 
    [2] = uiStackTouch( 0, ITEM_HEIGHT * (-1), GUI_LCD_RES_H, ITEM_HEIGHT, 2, tagItem0 ),
    [3] = uiDigital( IMG_Setting_Item_Icon_075_075_30, SIZE_SCALE(14), (ITEM_HEIGHT-SIZE_SCALE(50))/2, SIZE_SCALE(50), SIZE_SCALE(50) ),
    [4] = uiTextScroll( SIZE_SCALE(70), 0, GUI_LCD_RES_H - SIZE_SCALE(70), ITEM_HEIGHT, COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29),

    // 
    [5] = uiStackTouch( 0, ITEM_HEIGHT * 0, GUI_LCD_RES_H, ITEM_HEIGHT, 2, tagItem1 ),
    [6] = uiDigital( IMG_Setting_Item_Icon_075_075_30, SIZE_SCALE(14), (ITEM_HEIGHT-SIZE_SCALE(50))/2, SIZE_SCALE(50), SIZE_SCALE(50) ),
    [7] = uiTextScroll( SIZE_SCALE(70), 0, GUI_LCD_RES_H - SIZE_SCALE(70), ITEM_HEIGHT, COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29 ),

    // 
    [8] = uiStackTouch( 0, ITEM_HEIGHT * 1, GUI_LCD_RES_H, ITEM_HEIGHT, 2, tagItem2 ),
    [9] = uiDigital( IMG_Setting_Item_Icon_075_075_30, SIZE_SCALE(14), (ITEM_HEIGHT-SIZE_SCALE(50))/2, SIZE_SCALE(50), SIZE_SCALE(50) ),
    [10] = uiTextScroll( SIZE_SCALE(70), 0, GUI_LCD_RES_H - SIZE_SCALE(70), ITEM_HEIGHT, COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29 ),

    // 
    [11] = uiStackTouch( 0, ITEM_HEIGHT * 2, GUI_LCD_RES_H, ITEM_HEIGHT, 2, tagItem3 ),
    [12] = uiDigital( IMG_Setting_Item_Icon_075_075_30, SIZE_SCALE(14), (ITEM_HEIGHT-SIZE_SCALE(50))/2, SIZE_SCALE(50), SIZE_SCALE(50) ),
    [13] = uiTextScroll( SIZE_SCALE(70), 0, GUI_LCD_RES_H - SIZE_SCALE(70), ITEM_HEIGHT, COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29 ),

    // 
    [14] = uiStackTouch( 0, ITEM_HEIGHT * 3, GUI_LCD_RES_H, ITEM_HEIGHT, 2, tagItem4 ),
    [15] = uiDigital( IMG_Setting_Item_Icon_075_075_30, SIZE_SCALE(14), (ITEM_HEIGHT-SIZE_SCALE(50))/2, SIZE_SCALE(50), SIZE_SCALE(50) ),
    [16] = uiTextScroll( SIZE_SCALE(70), 0, GUI_LCD_RES_H - SIZE_SCALE(70), ITEM_HEIGHT, COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29 ),

    [17] = uiStackTouch( 0, ITEM_HEIGHT * 4, GUI_LCD_RES_H, ITEM_HEIGHT, 2, tagItem5 ),
    [18] = uiDigital( IMG_Setting_Item_Icon_075_075_30, SIZE_SCALE(14), (ITEM_HEIGHT-SIZE_SCALE(50))/2, SIZE_SCALE(50), SIZE_SCALE(50) ),
    [19] = uiTextScroll( SIZE_SCALE(70), 0, GUI_LCD_RES_H - SIZE_SCALE(70), ITEM_HEIGHT, COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29 ),

};



GUI_VIEW_DEF_NR( viewAppsList ) = 
{
    // name 
    .name = "AppsList",
    
    // init 
    .init = initialize,
    .display_func = display_callback_func,
    .refresh_func_check = display_refresh_func,
    .destroy_func = on_destroy_func,
    .set_parent_func = on_set_parent_func, 
    
    // input 
    .on_touch = on_touch,
    .on_button = on_button,
    .on_event_update = on_event_update,     
    
    .pWidgetlist =  (ui_widgets_type_t*)wglSetting ,
    .widget_cnt = WIDGET_COUNT( wglSetting ),

    .flag = GUI_VIEW_FLAG_FUNC | GUI_VIEW_FLAG_ALIVE_DELAY
};


static gui_fling_t              *m_applist_fling; 
static int16_t                  m_idx_of_first_item = -1;
static int16_t                  roll_last_total; 

/* 当前view初始化
// 需要指定前使用的控件组
// 自动更新间隔，不使用自动更新间隔则interval = 0

*/
static void initialize(gui_menu_t * menu, void* pvParameter)
{
    menu->interval = 0;
    menu->lifetime = VIEWS_LIFETIME_DEFAULT; 

    m_applist_fling = gui_fling_get();
    gui_fling_init_t init_obj = GUI_FLING_CONFIG( 0, 0, GUI_LCD_RES_H, GUI_LCD_RES_V, true, ROLL_V_MIN_OFFSET, ROLL_V_MAX_OFFSET, ITEM_HEIGHT, 100, NULL );    
    gui_fling_init( m_applist_fling, &init_obj );
    
    gui_group_scroll_reset();
    gui_share_refresh_part_reset();    

}
static void on_set_parent_func(gui_menu_t * menu, const gui_view_t * parent)
{
//    p_this_parent = parent;
}
/* 销毁函数
// 在销毁前处理当前信息，比如不允许销毁时，将当前界面状态保存为background。
// true:  允许销毁
// false: 不允许自动销毁，则GUI只关闭当前显示，输入事件仍由当前view进行处理。
          那么view判断当前状态为background时，作出对应background下的按键、触摸等事件反馈
*/
static uint32_t on_destroy_func(gui_menu_t * menu)
{
    roll_last_total = m_applist_fling->roll_offset_total;
    
    gui_fling_set_disable( m_applist_fling );     
    return GUI_DESTROY_RET_DEFAULT; 
}


/* 显示回调
   重新指定参数, 用来获取当前界面的控件组
   ** 当前界面有多个控件组时，应该有条件确定使用哪一组控件组 ** 
*/
static void display_callback_func(gui_menu_t * menu)
{
    ui_property_t prop;
    ui_widgets_type_t * widgets = menu->widgets;
    bool draw = false;


    widgets[1].visible = 0; 

    
    if( IS_REQ_SRC_CREATE( menu->source) )
    {
        draw = true;
        if( menu->source == GUI_VIEW_REQ_SRC_CREATE_RETURN ) // 上级返回时，使用上次滚动界面的偏移量
        {
            m_applist_fling->roll_offset_total = roll_last_total; 
        }

        // 创建 局部更新( 其他创建来源，不需要自动更新  )
        if( IS_REQ_SRC_CREATE_SINGLE( menu->source ) )// menu->source == GUI_VIEW_REQ_SRC_CREATE || menu->source == GUI_VIEW_REQ_SRC_CREATE_RETURN )
        {
//            gui_part_t part = GUI_PART_CONFIG_ALL( gui_share_refresh_part_get(), tagItemCount, 2*50 );
//            gui_part_create( &part );
        }
    }
    else // 保存偏移量
    {
        ui_widgets_type_t* p = &widgets[4]; 
        for(int16_t i = 0; i < tagItemCount; i ++)
        {                
            gui_group_scroll_set(i, m_idx_of_first_item + i, p->uiTextScroll.extension->textscroll.delta_x );
            p += 3; 
        }
        
        if( menu->source == GUI_VIEW_REQ_SRC_UPDATE_FLING )
        {
            draw = true; 
        }
    }
    if( menu->source == GUI_VIEW_REQ_SRC_UPDATE_MANUAL || menu->source == GUI_VIEW_REQ_SRC_UPDATE_FLING )
    {
        gui_core_lifetime_refresh( menu );
    }
    
    if( draw )
    {
        m_idx_of_first_item = -((m_applist_fling->roll_offset_total / ITEM_HEIGHT) + 1);
        gui_layout_update_general_container_calc( widgets, WIDGET_COUNT(wglSetting), m_applist_fling->item_delta, UI_WIDGET_TYPE_STACK );
        
        // 更新局部刷新的区域
//        ui_widgets_type_t* pw = &widgets[2];
//        for(int16_t i = 0 ; i < tagItemCount; i ++ )
//        {
//            int16_t x = pw->x + pw->uiStack.delta_x + SIZE_SCALE(70); // 文本坐标x， 70是文字相对位置x
//            int16_t y = pw->y + pw->uiStack.delta_y + 0;             // 文本坐标y,  30是文字相对位置y
//            gui_share_refresh_part_set( i, x, y, GUI_LCD_RES_H - SIZE_SCALE(70), ITEM_HEIGHT );
//            pw += 3;             
//        }
            
        // 更新item 内容 
        string_t const *pstr;
        int16_t index = m_idx_of_first_item; // item的序号
        int16_t idx_of_container = 2;  // container的开始序号
        for( int16_t i = 0; i < tagItemCount; i++)
        {
            // 序号无效， 不需要加载， 且不可见
            if( index < 0 || index >= IDX_MAXIMUM )
            {
                widgets[idx_of_container].visible = 0;
            }
            else 
            {
                // 内容可见
                widgets[idx_of_container ].visible = 1;

                // 更新图标
                prop.digital.idx = item_list[index].idx;
                gui_widget_update_prop( widgets + idx_of_container + 1, &prop );

                // 更新文字
                pstr = xapp_local_text_item_get( (int) item_list[index].text ) ;
                if( pstr != NULL ) 
                {
                    // 更新textscroll文本偏移量
                    prop.textscrolled.content = (void*)pstr->text;
                    prop.textscrolled.len = pstr->len;
                    prop.textscrolled.roll_delta_x = 0; // 更新当前偏移量
                    gui_widget_update_prop( widgets + idx_of_container + 2, &prop );   
                    
                    // 对应文本，当前位置+之前的偏移量
                   ui_widgets_type_t* ptextscroll = &widgets[idx_of_container + 2];
                   ptextscroll->uiTextScroll.extension->textscroll.nx = ptextscroll->x + gui_group_scroll_get( index ); // 对应item序号的偏移量
                   LOG3("item %d x = %d, nx = %d", index, ptextscroll->x, ptextscroll->uiTextScroll.extension->textscroll.nx ); 
                }            
            }
            index ++;
            idx_of_container += 3; // switch to next container index 
        }        

    }

//    if( menu->source == GUI_VIEW_REQ_SRC_UPDATE_PART )
//    {
//        // 更新 滚动文字坐标
//        int index = 4;
//        for(int i = 0; i < tagItemCount; i ++ )
//        {
//            prop.textscrolled.content = NULL;
//            prop.textscrolled.len = 0;
//            prop.textscrolled.roll_delta_x = -1; 
//            gui_widget_update_prop( &widgets[index], &prop );
//            index += 3; 
//        }
//    }

    
}

/* 刷新显示回调
   根据当前状态，重新绘制指定的控件组 
*/
static int display_refresh_func(gui_menu_t * menu)
{
    return 1;
}

static void on_touch(void* pvParameters, gui_menu_t * menu)
{
    tp_algo_evt_t* touch = (tp_algo_evt_t*)pvParameters;
    
    switch( touch->type )
    {
        case TP_ALGO_EVT_TYPE_DOWN:
        gui_fling_on_touch_press( m_applist_fling );
        break;

        case TP_ALGO_EVT_TYPE_UP:
        gui_fling_on_touch_release( m_applist_fling );
        break;

        case TP_ALGO_EVT_TYPE_CLICK:        
        GUI_WIDGET_LIST_CHECK_TOUCH( menu->widgets, WIDGET_COUNT(wglSetting), 0, 0, touch->x, touch->y, list_item_handler );
        break;

        case TP_ALGO_EVT_TYPE_HOLD:
        if( touch->deltaY != 0 )
        {
            gui_fling_set_roll( m_applist_fling, touch->deltaY );
        }
        break;

        case TP_ALGO_EVT_TYPE_DRAG_X:
        VALUES_SET( x, touch->deltaX );
        local_view_event_handler( tagApplistSelect_DragStartX );
        break;       

        case TP_ALGO_EVT_TYPE_SLIDE_L:
        local_view_event_handler( tagApplistSelect_ToRight );
        break;

        case TP_ALGO_EVT_TYPE_SLIDE_R:
        local_view_event_handler( tagApplistSelect_ToLeft );
        break;

        case TP_ALGO_EVT_TYPE_SLIDE_D:
        gui_fling_switch_item( m_applist_fling, 2, false );
        break;

        case TP_ALGO_EVT_TYPE_SLIDE_U:
        gui_fling_switch_item( m_applist_fling, 2, true );
        break;
        
        default:
        break;
    }
}
/* 界面的按键处理

*/
static void on_button(void* pvParameters, gui_menu_t * menu)
{
    kb_evt_t * kb = (kb_evt_t*)pvParameters;
    
    switch( kb->type ) 
    {
        case KB_EVT_CLICK:           
        case KB_EVT_SHORT_HOLD_READY:
        local_view_event_handler( tagApplistSelect_BackToWatchface );
        break;

        default:
        break;
    }
 
}

static void on_event_update(void * pvParameters, gui_menu_t * menu)
{
    custom_evt_t* p_evt = (custom_evt_t*)pvParameters;
    if( p_evt->type == CUSTOM_EVT_TYPE_LANGUAGE_CHANGE )
    {
        gui_view_create_return( &viewAppsList ); // 用create return, 还原位置   
    }
}

static void list_item_handler(uint32_t tag)
{
    int idx = (int)(tag + m_idx_of_first_item);
    if( idx < 0 || idx >= IDX_MAXIMUM )
    {
        return;
    }
    uint32_t itemtag = item_list[idx].tag;
    local_view_event_handler( itemtag );
}

static void local_view_event_handler( uint32_t tag )
{
    const datetime_t* pdt = xapp_clock_get_now();
    static uint8_t year,month,day,hour,minute;
    switch( tag )
    {
        case tagApplistSelectItemWatchfacePreview:
        gui_view_create_sub( &viewAppsList, &viewWatchfacePreview );
        break;
        case tagApplistSelectItemExercise:
        gui_view_create_sub( &viewAppsList, &viewExercise_Selection );
        break;
        case tagApplistSelectItemHeartrate:
        gui_view_create_sub( &viewAppsList, &viewApp_Heartrate );
        break;
//        case tagApplistSelectItemBloodpressure:
//        gui_view_create_sub( &viewAppsList, &viewApp_Bloodpressure );
//        break;
#if !defined(BLE_NO_CONNECT)
        case tagApplistSelectItemMessageList:
        gui_view_create_sub( &viewAppsList, &viewApp_MessageList );
        break;
#endif
        case tagApplistSelectItemBrightness:
        gui_view_create_sub( &viewAppsList, &viewSettings_Brightness );
        break;
#if !defined(BLE_NO_CONNECT)
//        case tagApplistSelectItemStopwatch:
//        gui_view_create_sub( &viewAppsList, &viewTools_Stopwatch );
//        break;
        case tagApplistSelectItemTimer:
        {
            const xapp_timer_desc_t* p_timer_desc = xapp_timer_get();
            if( p_timer_desc->started )
            {
                gui_view_create_sub( &viewAppsList, &viewTools_TimerElapsed );
            }
            else 
            {
                gui_view_create_sub( &viewAppsList, &viewTools_TimerSelect );
            }
        }
        break;
        case tagApplistSelectItemCamera:
        gui_view_create_sub( &viewAppsList, &viewTools_Camera );
        break;
        case tagApplistSelectItemMusicControl:
        gui_view_create_sub( &viewAppsList, &viewTools_MusicControl );
        break;
        case tagApplistSelectItemFindPhone:
        gui_view_create_sub( &viewAppsList, &viewNotif_FindPhone);
        break;
#endif
        case tagApplistSelectItemLanguage:
        gui_view_create_sub( &viewAppsList, &viewWatch_Language); 
        break;
        
        case tagApplistSelectItemTimeSet:
        {
        hour = pdt->hour;
        minute = pdt->minute;
        //gui_view_create_sub( &viewAppsList, &uiSetupTimeView );
        view_enter_setup_time(views_load_current_watchface(),TEXT_TAG_SET_TIME,&hour,&minute,xapp_clock_set_time);
        }
        break;
        case tagApplistSelectItemDateSet:
        {
        year = pdt->year;
        month = pdt -> month;
        day = pdt -> day;
        //gui_view_create_sub( &viewAppsList, &uiSetupDateView );
        view_enter_setup_date(views_load_current_watchface(),TEXT_TAG_SET_DATE,&year,&month,&day,xapp_clock_set_date);
        }
        break;
        
        case tagApplistSelectItemDeviceInformation:
        gui_view_create_sub( &viewAppsList, &viewDevInfo );
        break;
#if !defined(BLE_NO_CONNECT)
        case tagApplistSelectItemAppDownload:
        gui_view_create_sub( &viewAppsList, &viewDevInfo_AppLink );
        break;
#endif
        case tagApplistSelectItemSetUnwearTip:
        gui_view_create_sub( &viewAppsList, &viewSetUnwearTip );
        break;
        case tagApplistSelectItemMore:
        gui_view_create_sub( &viewAppsList, &viewSystem_Power );
        break;


        case tagApplistSelect_DragStartX:
        {
            int16_t deltaX = VALUES_GET(x);
            if( deltaX > 0 )
            {
                const xapp_exercise_desc_t* p_exercise_desc = xapp_exercise_get_desc();
                if( p_exercise_desc->started )
                {
                    gui_view_create_transition( &viewAppsList, views_load_exercise_record(), &manageTransition, deltaX, 0 );
                }
                else 
                {
                    gui_view_create_transition( &viewAppsList, &viewApp_Sleep, &manageTransition, deltaX, 0 );
                }
            }
            else 
            {
                gui_view_create_transition( &viewAppsList, views_load_current_watchface(), &manageTransition, deltaX, 0 );
            }
        }
        break;

        case tagApplistSelect_ToRight:
        {
            const xapp_exercise_desc_t* p_exercise_desc = xapp_exercise_get_desc();
            if( p_exercise_desc->started )
            {
                GUI_VIEW_CREATE_NAVIGATE_TO_RIGHT( &viewAppsList, views_load_exercise_record(), 0 );
            }
            else 
            {
                GUI_VIEW_CREATE_NAVIGATE_TO_RIGHT( &viewAppsList, &viewApp_Sleep, 0 );
            }
        }
        break;

        case tagApplistSelect_ToLeft:
        {
            GUI_VIEW_CREATE_NAVIGATE_TO_LEFT( &viewAppsList, views_load_current_watchface(), 0 );
        }
        break;

        case tagApplistSelect_BackToWatchface:
        gui_view_create( views_load_current_watchface() );
        break;
    }
    // gui_fling_set_disable( m_applist_fling ); 
}
